package com.fpmsa.officeweb.controller;


import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/upload")
public class UploadController {

    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    @PostMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(@RequestParam("userphoto") MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String realpath = "D:/fpmsaProject/" + "userphoto" +"/";
        String filePath = "/userphoto/";

        File fileDir = new File(realpath);

        // 文件夹不存在则创建
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }

        String newName = new Date().getTime() +"_"+filename;
        File tempFile = new File(fileDir.getPath()+File.separator+newName);
        file.transferTo(tempFile);


        Map<String, String> result = new HashMap<>();
        result.put("filename", filename);
        result.put("realName", tempFile.getName());
        result.put("relatPath", filePath + newName);
        return JSON.toJSONString(result).replace("\"","\'");

    }

    @PostMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(@RequestParam("userfile") MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String realpath = "D:/fpmsaProject/" + "userfile" +"/";
        String filePath = "/userfile/";

        File fileDir = new File(realpath);

        // 文件夹不存在则创建
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }

        String newName = new Date().getTime() +"_"+filename;
        File tempFile = new File(fileDir.getPath()+File.separator+newName);
        file.transferTo(tempFile);


        Map<String, String> result = new HashMap<>();
        result.put("filename", filename);
        result.put("realName", tempFile.getName());
        result.put("relatPath", filePath + newName);
        return JSON.toJSONString(result).replace("\"","\'");

    }

    //文件下载相关代码
    @RequestMapping("/download")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response){
        String fileName = "FileUploadTests.java";
        /*if (fileName != null) {
            //当前是从该工程的WEB-INF//File//下获取文件(该目录可以在下面一行代码配置)然后下载到C:\\users\\downloads即本机的默认下载的目录
            String realPath = request.getServletContext().getRealPath("//WEB-INF//");
            File file = new File(realPath, fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition",
                        "attachment;fileName=" +  fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }*/
        return null;
    }

}
